package datamining.parsers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;

import datamining.sequence.ItemSet;
import datamining.sequence.Sequence;
import datamining.sequence.SequenceFactory;

/**
 * @author xujinghu
 *
 * @param <ItemType>
 */

public class DatSeqParser<ItemType> implements SequenceParser<ItemType> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public LabelParser<ItemType> getNodeParser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<Sequence<ItemType>> parse(Reader reader,
			SequenceFactory<ItemType> factory)
			throws ParseException, IOException {
		// TODO Auto-generated method stub
		ArrayList<Sequence<ItemType>> transaction = new ArrayList<Sequence<ItemType>>();
		BufferedReader br = new BufferedReader(reader);	//输入流
		String temp = null;
		StringBuffer sb = new StringBuffer();	//缓冲区
		temp = br.readLine();
		
		while (temp != null) {
			String[] item_strs = temp.split(" ");	//用空格分割
			
			ArrayList<ItemSet<ItemType>> itemSets = new ArrayList<ItemSet<ItemType>>();
			
			for (int i = 0; i < item_strs.length; i++){
				ItemSet<ItemType> itemSet = new ItemSet<ItemType>();
				char[] item_chars = item_strs[i].toCharArray();
				
				for (int j = 0; j < item_chars.length; j++){
					itemSet.add((ItemType) Character.valueOf(item_chars[j]));
				}
				
				itemSets.add(itemSet);
				sb.append(item_strs[i] + " ");
			}
			
			transaction.add(factory.newSequence(itemSets));
			sb.append("\n");
			temp = br.readLine();
		}
		
		System.out.println("输入数据为：\n" + sb);
		System.out.println("转换后的数据为：");
		
		for(Sequence<ItemType> seq : transaction)
			System.out.println(seq);
		
		br.close();
	
		return transaction;
	}
	
	@Override
	public Collection<Sequence<ItemType>> parse(InputStream io,
			SequenceFactory<ItemType> factory)
			throws ParseException, IOException {
		// TODO Auto-generated method stub
		throw new ParseException("目前尚未实现此方法", 0);
	}

	@Override
	public Sequence<ItemType> parse(String input,
			SequenceFactory<ItemType> factory)
			throws ParseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String serialize(Sequence<ItemType> Sequence) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void serialize(OutputStream out,
			Collection<Sequence<ItemType>> sequences)
			throws IOException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Collection<Sequence<ItemType>> parse(Writer writer,
			SequenceFactory<ItemType> factory)
			throws ParseException, IOException {
		// TODO Auto-generated method stub
		return null;
	}

}
